package com.lanyou.cook.config;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.DBlink;
import com.lanyou.cook.entity.domain.DBlinkDataSource;
import com.lanyou.cook.entity.domain.DBlinkInterface;
import com.lanyou.cook.entity.domain.Field;
import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.cook.entity.domain.InterfaceExecutionResult;
import com.lanyou.cook.entity.domain.InterfaceSchedule;
import com.lanyou.cook.entity.domain.KettleDataSourceEx;
import com.lanyou.cook.entity.domain.SlaveServerLog;
import com.lanyou.cook.entity.domain.SystemInterface;
import com.lanyou.cook.entity.domain.SystemSource;
import com.lanyou.cook.entity.domain.userManagement.Organization;
import com.lanyou.cook.entity.domain.userManagement.Permission;
import com.lanyou.cook.entity.domain.userManagement.Role;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.entity.kettle.KettleDatabaseType;
import com.lanyou.cook.entity.kettle.KettleSlaveServer;
import com.lanyou.esb.entity.ClusterNode;
import com.lanyou.esb.entity.ExceptionErrorCodeSetting;
import com.lanyou.esb.entity.Ftp;
import com.lanyou.esb.entity.FtpLog;
import com.lanyou.esb.entity.Http;
import com.lanyou.esb.entity.IfAlarmWorkOrder;
import com.lanyou.esb.entity.IfAvgResTimes;
import com.lanyou.esb.entity.IfParameter;
import com.lanyou.esb.entity.StoreForward;
import com.lanyou.esb.entity.WsdlMetaData;
import com.lanyou.esb.entity.WsdlMetaDataDetail;

@Configuration
@Import(RepositoryRestMvcConfiguration.class)
public class SpringDataRestConfiguration extends RepositoryRestMvcConfiguration {

	@Override
	protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
		super.configureJacksonObjectMapper(objectMapper);
		// objectMapper.setDateFormat(new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
	}

	private static Class<?>[] exposeIdsFor() {
		Collection<Class<?>> result = new ArrayList<>();
		result.add(Field.class);
		result.add(FieldSystem.class);
		result.add(SystemInterface.class);
		result.add(InterfaceSchedule.class);
		result.add(DBlink.class);
		result.add(DBlinkInterface.class);
		result.add(DBlinkDataSource.class);
		result.add(KettleSlaveServer.class);
		result.add(KettleDatabaseType.class);
		result.add(KettleDataSourceEx.class);
		result.add(User.class);
		result.add(Organization.class);
		result.add(Role.class);
		result.add(Permission.class);
		result.add(SlaveServerLog.class);
		result.add(InterfaceExecutionResult.class);
		result.add(SystemSource.class);
		result.add(Http.class);
		result.add(Ftp.class);
		result.add(ExceptionErrorCodeSetting.class);
		result.add(WsdlMetaData.class);
		result.add(WsdlMetaDataDetail.class);
		result.add(IfParameter.class);
		result.add(IfAvgResTimes.class);
		result.add(IfAlarmWorkOrder.class);
		result.add(ClusterNode.class);
		result.add(FtpLog.class);
		result.add(StoreForward.class);
		return result.toArray(new Class<?>[result.size()]);
	}

	@Override
	protected void configureRepositoryRestConfiguration(
			RepositoryRestConfiguration config) {
		super.configureRepositoryRestConfiguration(config);

		config.setBaseUri(Constants.BASE_URL_API);
		config.setReturnBodyOnCreate(true);

		config.exposeIdsFor(exposeIdsFor());
	}

	// @Override
	// public BaseUri baseUri() {
	// return new BaseUri(Constants.BASE_URL_API);
	// }
}
